package attend.action.common;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import attend.UserContainer.User;
import attend.com.CodeMetrix;
import attend.com.Session;
import attend.dao.common.CommonDao;
import dao.Paging.PagingDAO;
import exception.BaseException;

public class CommonCodeAction extends Session {
	
	private String 							group_cd;			//대표코드
	private String 							group_nm;			//대표코드명
	
	private String							cd_no;				//하위 코드
	private String							cd_nm;				//하위 코드명
	
	private String 							use_yn;				//사용여부
	private String 							etc;				//비고
	
	private List<HashMap<String, String>>	list;
	
	private List<HashMap<String, String>>	listGroupCode;
	
	private CommonDao						commDao;
	
	private PagingDAO						pDao;
	
	private int 							rowCount;						//조회 건수
	private int 							cur_page		=		1;		//현재페이지
	private int start_row;
	private int end_row;
	
	private String							msg;
	private String 							group_cd_max;
	private HashMap<String, String> 		hm_high_cd;			//대표코드 정보
	
	private String							crud_type;				//CRUD TYPE
	private User user;
	
	/**
	 *Default	Constructor 
	 */
	public CommonCodeAction() {}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	/**
	 * <pre>
	 * 공통코드 테이블 조건 조회
	 * </pre>
	 * @exception BaseException
	 * @exception {@link SQLException}
	 * @since	2009/02/10
	 */
	public String execute() throws BaseException, SQLException {

		commDao	=	new CommonDao();
		
		//대표코드만 조회함.
		commDao.retrieveCommonGroupCd();
		listGroupCode	=	commDao.getList();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>
	 * 공통코드 테이블 결과조회
	 * </pre>
	 * @return
	 * @throws BaseException
	 * @throws SQLException 
	 */
	public String ajax_list_common_code() throws BaseException, SQLException {
		
		commDao			=	new CommonDao();
		//Total Row Count
		commDao.retrieveCommonTableRowCount(group_cd, cd_nm);
		
		rowCount	=	commDao.getRowCount();											//total row
		
		pDao			=	new PagingDAO(10 , 10 , rowCount , cur_page);				//페이징 처리 DAO
		
		start_row 		= 	pDao.getWriting_Start();			//시작 페이지 1
	    end_row   		= 	pDao.getWriting_End();				//끝페이지
	    
	    //페이징 추가되어 결과 조회
		commDao.retrieveCommonTable(group_cd, cd_nm , start_row , end_row);
		
		list	=	commDao.getList();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>
	 * 대표코드 POP-UP
	 * </pre>
	 * @return
	 * @throws BaseException
	 * @throws SQLException
	 */
	public String openPrimaryCode_p() throws BaseException , SQLException {
		
		System.out.println(group_cd);
		System.out.println(cd_no);
		
		commDao	=	new	CommonDao();
		
		//그룹코드와 하위코드가 있으면 수정화면이다.
		if (group_cd != null && cd_no != null) {
			
			crud_type	=	CodeMetrix.UPDATE;	//	update
			
			//대표코드 정보 조회
			commDao.retireveCommonGroupCodeByOne(group_cd);
			hm_high_cd	=	commDao.getHm_high_cd();
			
			group_cd	=	hm_high_cd.get("group_cd");	//대표코드
			group_nm	=	hm_high_cd.get("cd_nm");	//코드명
			cd_no		=	hm_high_cd.get("cd");		//하위코드	//0
			etc			=	hm_high_cd.get("etc");		//비고
			use_yn		=	hm_high_cd.get("use_yn");	//사용여부
			
			return SUCCESS;
		}
		
		//max 대표코드 +1 , max 대표코드
		commDao.maxCommTableGroupCd();
		
		group_cd_max		=	commDao.getMaxGroupCd();
		group_cd			=	commDao.getMaxGroupCdPlusOne();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>
	 * 하위코드 POP-UP
	 * </pre>
	 * @return
	 * @throws BaseException
	 * @throws SQLException
	 */
	public String openSubCode_p() throws BaseException , SQLException {
		System.out.println(group_cd);
		System.out.println(cd_no);
		
		commDao	=	new	CommonDao();
		
		//그룹코드와 하위코드가 있으면 수정화면이다.
		if (group_cd != null && cd_no != null) {
			
			crud_type	=	CodeMetrix.UPDATE;	//	update
			
			//대표코드 정보 조회
			commDao.retireveCommonSubGroupCode(group_cd , cd_no);
			hm_high_cd	=	commDao.getHm_high_cd();
			
			group_cd	=	hm_high_cd.get("group_cd");	//대표코드
			group_nm	=	hm_high_cd.get("group_nm");	//코드명
			cd_no		=	hm_high_cd.get("sub_cd");	//하위코드
			cd_nm		=	hm_high_cd.get("sub_nm");	//하위명	
			etc			=	hm_high_cd.get("etc");		//비고
			use_yn		=	hm_high_cd.get("use_yn");	//사용여부
			
			return SUCCESS;
		}
		
		//max 대표코드 +1 , max 대표코드
		commDao.maxCommTableGroupCd();
		
		group_cd_max		=	commDao.getMaxGroupCd();
		group_cd			=	commDao.getMaxGroupCdPlusOne();
		
		return SUCCESS;
	}
	
	/**
	 * <pre>
	 * 대표코드  -저장- 
	 * </pre>
	 * @return
	 * @throws BaseException
	 * @author Yang	D
	 * @throws SQLException 
	 * @since	2009/02/10
	 */
	public String insertPrimaryCode() throws BaseException, SQLException {
		
		commDao	=	new CommonDao();
		
		//대표코드 수정일 경우
		if (crud_type.equals("U")) {
			commDao.updateHighCode(group_nm , etc , use_yn , group_cd);
			msg	=	commDao.getMsg();
			
			return SUCCESS;
		}
		
		//대표코드를 우선 삭제한다.
		//초최 입력시 대표코드가 없으므로 상관업게된다.
		//수정모드일 경우 삭제를 해야하기 때문에 넣었다.
		commDao.deleteHighCodeByOne(group_cd);
		
		commDao.insertCommonTableGroupCd(group_nm, etc	, use_yn);
		msg	=	commDao.getMsg();		//성공시에 OK
		
		return SUCCESS;
	}
	
	
	/*********************************GETTER/SETTER*************************************************/
	public List<HashMap<String, String>> getList() {
		return list;
	}

	public void setList(List<HashMap<String, String>> list) {
		this.list = list;
	}

	public List<HashMap<String, String>> getListGroupCode() {
		return listGroupCode;
	}

	public void setListGroupCode(List<HashMap<String, String>> listGroupCode) {
		this.listGroupCode = listGroupCode;
	}

	public String getCd_nm() {
		return cd_nm;
	}

	public void setCd_nm(String cd_nm) {
		this.cd_nm = cd_nm;
	}

	public String getGroup_cd() {
		return group_cd;
	}

	public void setGroup_cd(String group_cd) {
		this.group_cd = group_cd;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getCur_page() {
		return cur_page;
	}

	public void setCur_page(int cur_page) {
		this.cur_page = cur_page;
	}

	public int getStart_row() {
		return start_row;
	}

	public void setStart_row(int start_row) {
		this.start_row = start_row;
	}

	public int getEnd_row() {
		return end_row;
	}

	public void setEnd_row(int end_row) {
		this.end_row = end_row;
	}

	public PagingDAO getPDao() {
		return pDao;
	}

	public void setPDao(PagingDAO dao) {
		pDao = dao;
	}

	public String getEtc() {
		return etc;
	}

	public void setEtc(String etc) {
		this.etc = etc;
	}

	public String getCd_no() {
		return cd_no;
	}

	public void setCd_no(String cd_no) {
		this.cd_no = cd_no;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public String getGroup_nm() {
		return group_nm;
	}

	public void setGroup_nm(String group_nm) {
		this.group_nm = group_nm;
	}

	public String getUse_yn() {
		return use_yn;
	}

	public void setUse_yn(String use_yn) {
		this.use_yn = use_yn;
	}

	public String getGroup_cd_max() {
		return group_cd_max;
	}

	public void setGroup_cd_max(String group_cd_max) {
		this.group_cd_max = group_cd_max;
	}

	public HashMap<String, String> getHm_high_cd() {
		return hm_high_cd;
	}

	public void setHm_high_cd(HashMap<String, String> hm_high_cd) {
		this.hm_high_cd = hm_high_cd;
	}

	public String getCrud_type() {
		return crud_type;
	}

	public void setCrud_type(String crud_type) {
		this.crud_type = crud_type;
	}

	@Override
	public void setUser(User user) {
		this.user = user;
		
	}

	public User getUser() {
		return user;
	}	
	/*********************************GETTER/SETTER*************************************************/
}
